/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.LogRemarkDao;
import com.qdu.entity.LogRemark;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lenovo
 */
public class LogRemarkDaoImpl extends BaseDaoImpl implements LogRemarkDao{

    @Override
    public List<LogRemark> selectAllRemarkByLogId(String id) {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<LogRemark> list=new ArrayList();
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("select * from LogComment where Lid=?");
            ps.setString(1, id);
            rs=ps.executeQuery();
            while(rs.next())
            {
                list.add(new LogRemark(rs.getString(2),rs.getString(3),rs.getString(4),rs.getDate(5),rs.getString(6),rs.getInt(7)));
            }
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public LogRemark selectFirstRemarkByLogId(String id) {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        LogRemark remark=new LogRemark();
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("select top 1 * from LogComment where Lid=?");
            ps.setString(1,id);
            rs=ps.executeQuery();
            if(rs.next()){
                remark.setId(rs.getString(2));
                remark.setLogId(rs.getString(3));
                remark.setUserName(rs.getString(4));
                remark.setTime(rs.getDate(5));
                remark.setContent(rs.getString(6));
                remark.setAgree(rs.getInt(7));
            }
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(rs, ps, con);
        }
        return remark;
    }
     @Override
    public int executeUpdate(String sql, Object... params) {
        return super.executeUpdate(sql, params); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int deleteRemarkById(String id) {
        Connection con=null;
        PreparedStatement ps=null;
        int rows=0;
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("delete from LogComment where LCid=?");
            ps.setString(1, id);
            rows=ps.executeUpdate();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(null, ps, con);
        }
        return rows;
        
    }

    @Override
    public int addRemark(LogRemark remark) {
        Connection con=null;
        PreparedStatement ps=null;
        int rows=0;
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("insert into LogComment(LCid,Lid,Uid,LCcontent,LCagree) values(?,?,?,?,?)");
            ps.setString(1,"1");
            ps.setString(2,remark.getLogId());
            ps.setString(3,remark.getUserName());
            ps.setString(4,remark.getContent());
            ps.setInt(5,remark.getAgree());
            rows=ps.executeUpdate();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(null, ps, con);
        }
        return rows;
        
    }

    @Override
    public int modifyRemark(String id) {
        Connection con=null;
        PreparedStatement ps=null;
        int rows=0;
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("update LogComment set LCagree=LCagree+1 where LCid=?");
            ps.setString(1, id);
            rows=ps.executeUpdate();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(null, ps, con);
        }
        return rows;
    }
    
}
